<?php
	session_start();
	require_once('config.php');
?>
<!DOCTYPE html>
<html>
	<head>
		<?php
			require_once('scripts/head_tags.php');
		?>
	</head>
	<body>
		<h1>
			<img src="resources/images/alarm.png" alt="icon" /> Easy Security 
		</h1>
		<?php
			if($_SESSION['logged'] == false)
			{
		?>
			<section id="submit_form">
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>?action=login" method="post">
					<label for="username">
						Username:
					</label>
					<input type="text" name="username" id="username" autofocus="autofocus" />
					<label for="password">
						Password:
					</label>
					<input type="password" name="password" id="password" />
					<input type="submit" value="Login >" />
				</form>
				<?php
					$action = $_GET['action'];
					
					if($action == 'login')
					{
						if(isset($_POST['username']) && isset($_POST['password']))
						{
							$username = $_POST['username'];
							
							$password = $_POST['password'];
							
							if($username == USERNAME && $password == PASSWORD)
							{
								$_SESSION['logged'] = true;
								$_SESSION['username'] = $username;
								$_SESSION['password'] = $password;
								header('Location: '.INDEX.'');
							}
						}
					}
				?>
			</section>
		<?php
			}
			if($_SESSION['logged'] && $_SESSION['username'] == USERNAME && $_SESSION['password'] == PASSWORD)
			{
				echo '<h1>~ Reports from today ~</h1><hr />';
				
				require_once('config.php');
				
				$connect = new DbConnector();
				
				$today_timestamp = strtotime("today");
				
				$select = $connect->query("SELECT * FROM ".$TABLES['detection']." WHERE unix_time > ".$today_timestamp." ORDER BY unix_time DESC limit 20");
				
				if($connect->numRows($select))
				{
					while($row = $connect->fetchObject($select))
					{
						echo $row->date_time.'<br />';
					}
				}
				else
				{
					echo 'There is no data for movement that has been detected!';
				}
				
				echo '<hr />';
				
				require_once('scripts/footer.php');
			}
		?>
	</body>
</html>